package com.top.mall.main.mine

import android.animation.Animator
import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.blankj.utilcode.util.BarUtils
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.app.SPSConstance
import com.top.mall.jetpack.vm.CouponViewModel
import kotlinx.android.synthetic.main.activity_mine_integral.*
import kotlinx.android.synthetic.main.include_integral_head.*

class MineIntegralActivity : OwnerViewModeActivity<CouponViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineIntegralActivity::class.java)
            activity.startActivity(intent)
        }
    }


    override fun viewModel(): CouponViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_integral

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMineIntegralBar)
    }

    override fun initView() {

        BarUtils.setStatusBarLightMode(this, false)

        ivMineIntegralBack.setOnClickListener {
            onBackPressed()
        }

        supportFragmentManager(
            IntegralEarnedFragment.newInstance(), isToBackStack = false
        )

        tvIntegralChange.setOnClickListener {
            supportFragmentManager(IntegralChangeFragment.newInstance())
        }

        supportFragmentManager.registerFragmentLifecycleCallbacks(object :
            FragmentManager.FragmentLifecycleCallbacks() {
            override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
                super.onFragmentResumed(fm, f)
                if (f is IntegralEarnedFragment) {
                    tvIntegralChange.visibility = View.VISIBLE
                    tvIntegralChange.animate().scaleX(1f).scaleY(1f).alpha(1f).setDuration(300)
                        .setListener(null).start()
                    tvMineIntegralTitle.setText(R.string.my_integral)
                }
                if (f is IntegralChangeFragment) {
                    tvIntegralChange.animate().scaleX(0f).scaleY(0f).alpha(0.5f).setDuration(300)
                        .setListener(object : Animator.AnimatorListener {
                            override fun onAnimationRepeat(animation: Animator?) {

                            }

                            override fun onAnimationEnd(animation: Animator?) {
                                tvIntegralChange.visibility = View.GONE
                            }

                            override fun onAnimationCancel(animation: Animator?) {

                            }

                            override fun onAnimationStart(animation: Animator?) {

                            }
                        }).start()
                    tvMineIntegralTitle.setText(R.string.exchange_coupon)
                }
            }
        }, false)
    }

    override fun initData() {
        SPSConstance.userInfo?.let {
            tvIntegralCount.text = "${it.score}"
        }
    }

    private fun supportFragmentManager(fragment: Fragment, isToBackStack: Boolean = true) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (isToBackStack) {
            transaction.setCustomAnimations(
                R.animator.slide_right_in, R.animator.slide_left_out,
                R.animator.slide_left_in, R.animator.slide_right_out
            )
            transaction.replace(R.id.flMineIntegralBody, fragment)
            transaction.addToBackStack(null)
        } else {
            transaction.add(R.id.flMineIntegralBody, fragment)
        }
        transaction.commit()
    }

}